package it.scambiocorse.rowmapper;

import it.scambiocorse.model.UtenteType;
import it.scambiocorse.utils.Utils;

import java.sql.ResultSet;
import java.sql.SQLException;

import org.apache.log4j.Logger;
import org.springframework.jdbc.core.RowMapper;

public class UtenteRowMapper implements RowMapper {
	final Logger logger = Logger.getLogger(getClass());

  public Object mapRow(ResultSet rs, int row) throws SQLException {
  	String metodo ="mapRow(...)";
    UtenteType u= new UtenteType();
    try {

    if (row > -1) { // PROVENGO DALLA TABELLA
	        u.setIdUser(rs.getInt("id_user"));
	        u.setNome(Utils.getValid(rs.getString("nome")));
	        u.setCognome(Utils.getValid(rs.getString("cogn")));
	        u.setNote(Utils.getValid(rs.getString("note")));
	        u.setEmail(Utils.getValid(rs.getString("email")));
	        u.setAccessoPubblico((rs.getShort("accesso_pub") > 0));
	        u.setActivation((Utils.getValid(rs.getString("activation"))));

	
	    } else {
	    	u.setIdUser(rs.getInt("id_user_inse"));
	        u.setNome(Utils.isValid(rs.getString("nome_user")) ? rs.getString("nome_user") : "" );
	        u.setCognome(Utils.isValid(rs.getString("cogn_user")) ? rs.getString("cogn_user") : "" ); 
	        u.setNote(Utils.isValid(rs.getString("note_user")) ? rs.getString("note_user") : "" );  
	        u.setEmail(Utils.isValid(rs.getString("email_user")) ? rs.getString("email_user") : "" ); 
	        
	
	    }
	    
	    u.setLogin(Utils.getValid(rs.getString("login")));
	    u.setRagiSoci(rs.getString("ragi_soci"));
	    u.setCrediti(rs.getInt("crediti"));
	    u.setTel(Utils.getValid(rs.getString("tel")));
	} catch (SQLException e) {
		if (e.getSQLState().equals("S0022")) {
			u = null;
			logger.debug("Colonna non trovata, mappaggio non necessario");
		} else {
			throw e;
		}
	}
    return u;
  }
}
